<template>
  <div style="text-align: left">
    <v-md-editor
      v-model="value"
      :style="{fontSize: '18px'}"
      :disabled-menus="[]"
      :include-level="[1,2,3,4]"
      height='90vh'
      @change="change"
      @copy-code-success="handleCopyCodeSuccess"
      @upload-image="handleUploadImage"
    />
  </div>
</template>

<script>
import { uploadFileImage } from '@/api/image'

export default {
  name: 'MarkdownEditor',
  props: ['modelValue'],
  computed: {
    value: {
      get () {
        return this.modelValue
      },
      set (value) {
        this.$emit('update:modelValue', value)
      }
    }
  },
  setup () {
    return {
    }
  },
  methods: {
    // 复制成功
    handleCopyCodeSuccess (code) {
      this.$emit('copy-code-success', code)
      console.log(code)
    },
    // 图片上传
    handleUploadImage (event, insertImage, files) {
      // 拿到 files 之后上传到文件服务器，然后向编辑框中插入对应的内容
      console.log(files)
      uploadFileImage(files[0]).then((val) => {
        console.log(val)
        insertImage({
          url: val.data,
          desc: files[0].name,
          width: '300',
          height: 'auto'
        })
      })
    },
    change (even) {
      this.$emit('change', even)
    }
  }
}
</script>

<style scoped>
</style>
